package com.tzr.dao;

import com.tzr.bean.User;
import com.tzr.bean.Weibo;
import com.tzr.util.DruidUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserDaoImpl extends DruidUtil implements UserDao {
    @Override
    public List<Weibo> getAll() {
        List list = new ArrayList();
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            connection = getConnection();
            String sql = " select uname,pinglun from weibo";
            preparedStatement = connection.prepareStatement(sql);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
               Weibo weibo= new Weibo();
               weibo.setUname(resultSet.getString("uname"));
               weibo.setPinglun(resultSet.getString("pinglun"));
               list.add(weibo);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            close(connection, preparedStatement, resultSet);
        }
        return  list;
    }

    @Override
    public User login(String uname, String upassword) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        User user = new User();
        try {
            connection = getConnection();
            String sql = "select * from user where uname=? and upassword=?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,uname);
            preparedStatement.setString(2,upassword);
            resultSet = preparedStatement.executeQuery();
           while (resultSet.next()){
               user.setUname(resultSet.getString("uname"));
               user.setUpassword(resultSet.getString("upassword"));
           }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            close(connection, preparedStatement, resultSet);
        }
        return user;
    }

    @Override
    public int Zan() {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        Weibo weibo = new Weibo();
        int zan=0;
        try {
            connection = getConnection();
            String sql = "select dianzan from dianzan ";
            preparedStatement = connection.prepareStatement(sql);
            resultSet=preparedStatement.executeQuery();
            while(resultSet.next()){
                weibo.setZan(resultSet.getInt("dianzan"));
            }
            zan=weibo.getZan();
            return  zan;
        }catch (SQLException e) {
            e.printStackTrace();
        }finally {
            close(connection,preparedStatement,resultSet);
        }
        return zan;
    }

    @Override
    public boolean DianZan() {
        Connection connection = null;
        PreparedStatement preparedStatement = null;


        try {
            connection = getConnection();
            String sql = "update dianzan set dianzan=dianzan+1 ";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.executeUpdate();
        }catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                preparedStatement.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            try {
                connection.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }

        return false;
    }

    @Override
    public boolean PingLun(String uname,String pinglun) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;

        Weibo weibo = new Weibo();
        try {
            connection = getConnection();
            String sql = "insert into weibo(uname,pinglun) values(?,?)";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, uname);
            preparedStatement.setString(2, pinglun);
            preparedStatement.executeUpdate();
        }catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                preparedStatement.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            try {
                connection.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        return false;
    }
}
